
from .api_mysql import ApiMysqlclass

from flask import Flask, request

from . import api_table

app = Flask(__name__)

@api_table.route('/company', methods = ['GET', 'POST'])

def company_api():

    if request.method == 'GET':

        page = request.args.get('page')

        limit = request.args.get('limit')

        party = request.args.get('party')

        sql = 'select * from company limit %s,%s '

        return ApiMysqlclass().get_return(sql, page, limit, party)

    if request.method == 'POST':

        data_post = request.form

        info = (
            data_post.get('company_name'),
            data_post.get('product_name'),
            data_post.get('need'),
            data_post.get('linkman'),
            data_post.get('phone'),
            data_post.get('party'),
            data_post.get('pass_word'),
        )

        sql = '''INSERT INTO company
        (company_name, product_name, need, linkman, phone, party, pass_word)
        values
        (%s,%s,%s,%s,%s,%s,%s);       
        '''

        return ApiMysqlclass().post_return(sql, info)

@api_table.route('/company/<int:ID>', methods = ['GET', 'PUT'])
    
def company_id(ID):
    
    if request.method == 'GET':

        sql = 'SELECT * FROM company WHERE id=%s'
    
        return ApiMysqlclass().get_return_id(sql, ID)

    if request.method == 'PUT':

        data_put = request.form

        info = (
            data_put.get('company_name'),
            data_put.get('product_name'),
            data_put.get('need'),
            data_put.get('linkman'),
            data_put.get('phone'),
            data_put.get('party'),
            data_put.get('pass_word'),
            ID
        )

        sql = '''UPDATE company SET company_name=%s, product_name=%s, 
            need=%s, linkman=%s, phone=%s, party=%s, pass_word=%s WHERE id=%s'''

        return ApiMysqlclass().put_return(sql, info)

@api_table.route('/consumption', methods = ['GET', 'POST'])

def consumption_api():

    if request.method == 'GET':

        page = request.args.get('page')

        limit = request.args.get('limit')

        sql = 'select * from consumption limit %s,%s'

        return ApiMysqlclass().get_return(sql, page, limit)

    if request.method == 'POST':

        data_post = request.form

        info = (
            data_post.get('company_id'),
            data_post.get('consumption_url'),
            data_post.get('consumption_surplus'),
        )

        sql = '''INSERT INTO consumption
        (company_id, consumption_url, consumption_surplus)
        values
        (%s,%s,%s);       
        '''

        return ApiMysqlclass().post_return(sql, info)

@api_table.route('/consumption/<int:ID>', methods = ['GET'])
    
def consumption_id(ID):
    
    if request.method == 'GET':

        sql = 'SELECT * FROM consumption WHERE id=%s'
    
        return ApiMysqlclass().get_return_id(sql, ID)


if __name__ == '__main__':
    
    app.run(debug = True)
    